Imports MTV3D65.CONST_TV_KEY

Module modInput
    Public absMouse, MouseGrid As Point
    Public Mouse As PointF
    Public MouseRoll As Single 'Mouse positions.
    Public MouseB1, MouseB2, MouseB3 As Boolean 'Mouse butttons.

    Public CurrentTile As Integer = 0 'The current tile.
    Public CurrentHome As Integer = 0
    Public Mb1 As Boolean

    Public KeyF11 As Boolean = True
    Public KeyF5 As Boolean = True

    'This well check the input.
    Public Sub CheckInput()
        'Get the mouse position and movent and the buttons.
        Inp.GetAbsMouseState(absMouse.X, absMouse.Y, MouseB1, MouseB2, MouseB3)
        Inp.GetMouseState(Mouse.X, Mouse.Y, 0, 0, 0, 0, MouseRoll)

        'Get the mouse in grid position.
        MouseGrid.X = System.Math.Round(absMouse.X * 0.0625) '* 0.0625 or / 16
        MouseGrid.Y = System.Math.Round((absMouse.Y - 100) * 0.0625)
        'Clip to game.
        If MouseGrid.X < 0 Then MouseGrid.X = 0
        If MouseGrid.Y < 0 Then MouseGrid.Y = 0
        If MouseGrid.X > MapSize Then MouseGrid.X = MapSize
        If MouseGrid.Y > MapSize Then MouseGrid.Y = MapSize

        'Select the next tile baist on the mouse wheel.
        CurrentTile += MouseRoll * 0.01
        If CurrentTile < 0 Then
            CurrentTile = TileTex.Length - 1
        ElseIf CurrentTile > TileTex.Length - 1 Then
            CurrentTile = 0
        End If

        'Place tile.
        If MouseB1 Then

            'Get the path finding unmber.
            Dim texName As String = Tex.GetTextureInfo(TileTex(CurrentTile)).Name

            'Check to see if we added something to the home.
            Select Case LCase(texName)
                Case "kitchen_1"
                    Homes(CurrentHome).Kitchen.GridPosition = MouseGrid
                Case "bedroom_1"
                    Homes(CurrentHome).BedRoom.GridPosition = MouseGrid
                Case "diningroom_1"
                    Homes(CurrentHome).DiningRoom.GridPosition = MouseGrid
                Case "npc_-1"
                    If Mb1 = True Then NPCs.Add(New NPC(MouseGrid, Homes(CurrentHome), Homes(CurrentHome).BedRoom, TileTex(CurrentTile)))
                    GoTo ExitTile
            End Select

            Map(MouseGrid.X, MouseGrid.Y) = Right(texName, texName.Length - InStr(texName, "_"))
            Tile(MouseGrid.X, MouseGrid.Y) = TileTex(CurrentTile)
            World_Render()


ExitTile:
            Mb1 = False
        ElseIf Not MouseB1 And Not Mb1 Then
            Mb1 = True

        End If

        'Change current home.
        If Inp.IsKeyPressed(TV_KEY_1) Then CurrentHome = 0
        If Inp.IsKeyPressed(TV_KEY_2) Then CurrentHome = 1
        If Inp.IsKeyPressed(TV_KEY_3) Then CurrentHome = 2

        'Save
        If Inp.IsKeyPressed(TV_KEY_F11) And KeyF11 Then
            SaveMap()

            KeyF11 = False
        ElseIf Not Inp.IsKeyPressed(TV_KEY_F11) And Not KeyF11 Then
            KeyF11 = True
        End If
        'Open.
        If Inp.IsKeyPressed(TV_KEY_F5) And KeyF5 Then
            OpenMap()

            KeyF5 = False
        ElseIf Not Inp.IsKeyPressed(TV_KEY_F5) And Not KeyF5 Then
            KeyF5 = True
        End If

        'Exit when Escape is pressed.
        If Inp.IsKeyPressed(TV_KEY_ESCAPE) = True Then
            ExitLoop = True 'Exit
        End If


    End Sub
End Module
